###############
NAME=pkgr
VERSION=0.1
ARCH=$(shell uname -p)
BUILD_DIR=build
SOURCE_PACKAGE=${BUILD_DIR}/${NAME}-${VERSION}.tar.gz
###### Binaries
MOCK=mock
PIP=pip
NOSETESTS=nosetests
###### RPM
include Makefile.rpm.in
####### Mock
MOCK_CONFIG=fedora-17-${ARCH}
MOCK=${QUIET_PREFIX}/usr/bin/mock -q -r ${MOCK_CONFIG}
MOCK_ROOT=/var/lib/mock/${MOCK_CONFIG}/root
MOCK_RESULT=/var/lib/mock/${MOCK_CONFIG}/result
MOCK_REQUIRED_PACKAGES=python-progressbar python-pip tar gzip createrepo PyYAML python-setuptools python-lxml python-urlgrabber rpm-python python-matplotlib numpy python-devel gcc

include Makefile.in

.PHONY:  all docs rpm nosetests jenkins tests srpm tests pythonsolv

all: ${SOURCE_PACKAGE} rpm srpm

# make html docs
docs:
	$(call info_print,Building documentation...)
	make -C docs clean
	make -C docs html
	$(call info_print,Opening in browser...)
	xdg-open docs/build/html/index.html
	
${BUILD_DIR}:
	mkdir -p $@
	
# build a source tar.gz
source ${SOURCE_PACKAGE}: ${BUILD_DIR}
	$(call info_print,Building source distribution...)
	@python setup.py --quiet sdist --dist-dir ${BUILD_DIR}
	
# build a spec with the version/release
build/${NAME}.spec: ${RPMSPEC}
	$(call info_print,Building spec file...)
	sed "s/Version:        .*/Version: ${VERSION}/g" ${RPMSPEC} > $@
	sed -i "s/Release:        1%{?dist}/Release: ${RPMRELEASE}%{?dist}/g"  $@
	
# build an srpm
srpm ${RPMSRPMFILE}: ${SOURCE_PACKAGE} build/${NAME}.spec
	$(call info_print,Building srpm...)
	${MOCK} --buildsrpm --spec build/${NAME}.spec --sources ${BUILD_DIR}
	cp ${MOCK_RESULT}/*src.rpm ${BUILD_DIR}/
	
# build an rpm
rpm ${RPMFILE}: ${RPMSRPMFILE}
	$(call info_print,Installing build deps...)
	${MOCK} --installdeps $^ --no-cleanup-after
	$(call info_print,Building rpm...)
	${MOCK}  --no-clean --rebuild $^
	@cp ${MOCK_RESULT}/*.rpm build/
	
# creates a mock env
mockenvironment: 
	$(call info_print,Preparing a mock environment...)
	${MOCK} --clean
	${MOCK} --init
	# install system deps
	${MOCK} --install ${MOCK_REQUIRED_PACKAGES} --no-cleanup-after
	
# used for jenkins build server
# run tests in mock, copy out coverage and test results
# a mock is used so we can install test tool into the root
# best thing i could cook up for now
jenkins: ${SOURCE_PACKAGE} mockenvironment testdata/pylint.out
	$(call info_print,Running tests in a mock environment...)
	${MOCK} --shell "rm -rf /test/*"
	${MOCK} --shell "mkdir -p /test"
	# copy source in
	${MOCK} --copyin ${SOURCE_PACKAGE} /test/
	${MOCK} --cwd=/test --shell "cd /test && tar xvf *.tar.gz"
	# run tests. override pip to use the distros (fedora)
	${MOCK} --cwd=/test --shell "PIP=pip-python make -e -C /test/${NAME}-${VERSION} testdata/coverage.xml"
	mkdir -p testdata
	# copy testdata out of mock
	${MOCK} --copyout /test/${NAME}-${VERSION}/testdata/coverage.xml testdata/coverage.xml
	${MOCK} --copyout /test/${NAME}-${VERSION}/testdata/nosetests.xml testdata/nosetests.xml

# copies pkgr directly to the mock environment
mockshell: 
	$(call info_print,Running a mock shell suitable to test/run ${name}...)
	sudo rsync -avrpp ${NAME}/ ${MOCK_ROOT}/usr/lib/python2.7/site-packages/${NAME}/
	sudo cp bin/${NAME} ${MOCK_ROOT}/usr/bin
	${MOCK} --shell "mkdir -p /etc/pkgr"
	sudo cp config/fedora.repo.yaml ${MOCK_ROOT}/etc/pkgr/
	sudo cp config/fedora-updates.repo.yaml ${MOCK_ROOT}/etc/pkgr/
	sudo cp config/pkgr.yaml ${MOCK_ROOT}/etc/pkgr/pkgr.yaml
	${MOCK} --shell

# installs pkgr to a mock environment
mockinstall: ${RPMFILE}
	${MOCK} --clean
	${MOCK} --init
	${MOCK} --install ${RPMFILE}

mockuse:
	${MOCK} --shell
	
mockclean:
	${MOCK} --clean
	${MOCK} --init
	
clean:
	rm -Rf build
	rm -Rf testdata
	
# used for developer testing
# rpm need root privileges to do a chroot
tests:
	$(call info_print,Running tests using system nose...)
	fakechroot fakeroot /usr/sbin/chroot / make -C $(shell pwd) nosetests
	
nosetests:
	nosetests -v pkgr
